package ldh.im.web.controller;

import java.util.Date;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import ldh.common.PageResult;
import ldh.common.json.JsonViewFactory;
import ldh.common.mvc.DateEditor;
import ldh.im.business.pojo.Tenant;
import ldh.im.business.pojo.where.TenantWhere;
import ldh.im.web.service.TenantService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author: ldh
* @date: 2021-06-06 10:17:43
*/
@Controller
@RequestMapping("tenant")
public class TenantController  {

	@Resource(name = "tenantService")
	private TenantService tenantService;
	
	/**
	 * 字符串转化成时间
	 */
	@InitBinder  
	protected void initBinder(HttpServletRequest request,  
	                              ServletRequestDataBinder binder) throws Exception {  
	    //对于需要转换为Date类型的属性，使用DateEditor进行处理  
	    binder.registerCustomEditor(Date.class, new DateEditor());
	}

	@RequiresPermissions("imTenant:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save")
    public String save(@ModelAttribute Tenant tenant) throws Exception {
    	Objects.requireNonNull(tenant);
    	if (tenant.getId() == null) {
			tenantService.insert(tenant);
    	} else {
			tenantService.updateById(tenant);
    	}
    	return "redirect:/tenant/list";
    }

	@RequiresPermissions("imTenant:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save/json")
    @ResponseBody
    public String saveJson(@ModelAttribute TenantWhere tenant) throws Exception {
    	Objects.requireNonNull(tenant);
    	if (tenant.getId() == null) {
			tenantService.insert(tenant);
		} else {
			tenantService.updateById(tenant);
		}
        return JsonViewFactory.create()
				.toJson();
    }

	@RequiresPermissions("imTenant:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/toEdit/{id}")
    public String toEdit(@PathVariable Long id, Model model) throws Exception {
		Tenant tenant = tenantService.getById(id);
    	model.addAttribute("tenant", tenant);
		model.addAttribute("active", "tenant");
        return "tenant/tenantEdit";
    }

	@RequiresPermissions("imTenant:toAdd")
	@RequestMapping(method = RequestMethod.GET, value = "/toAdd")
    public String toAdd(Model model) throws Exception {
		model.addAttribute("active", "tenant");
        return "tenant/tenantEdit";
    }

	@RequiresPermissions("imTenant:view")
	@RequestMapping(method = RequestMethod.GET, value = "/view/{id}")
    public String view(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	Tenant tenant = tenantService.getById(id);
    	model.addAttribute("tenant", tenant);
		model.addAttribute("active", "tenant");
        return "tenant/tenantView";
    }

	@RequiresPermissions("imTenant:view")
	@ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/view/json/{id}")
    public String viewJson(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	Tenant tenant = tenantService.getById(id);
    	return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", tenant)
				.toJson();
    }

	@RequiresPermissions("imTenant:list")
	@RequestMapping(method = RequestMethod.GET, value = "/list")
	public String list(@ModelAttribute TenantWhere tenantWhere, Model model) {
		tenantWhere.setOrder("id desc");
		PageResult<Tenant> tenants = tenantService.findByTenantWhere(tenantWhere);
		model.addAttribute("tenants", tenants);
		model.addAttribute("active", "tenant");
		model.addAttribute("tenant", tenantWhere);
		return "tenant/tenantList";
	}

	@RequiresPermissions("imTenant:list")
	@RequestMapping(method = RequestMethod.GET, value = "/list/json")
	@ResponseBody
	public String listJson(@ModelAttribute TenantWhere tenantWhere, Model model) {
		tenantWhere.setOrder("id desc");
		PageResult<Tenant> tenants = tenantService.findByTenantWhere(tenantWhere);
		return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", tenants)
				.toJson();
	}

	@RequiresPermissions("imTenant:delete")
	@RequestMapping(method = RequestMethod.GET, value = "/deleteById/json/{id}")
	@ResponseBody
	public String deleteJsonById(@PathVariable("id")Long id) {
		Tenant tenant = tenantService.getById(id);
        Objects.requireNonNull(tenant);
		tenantService.deleteById(id);
        return JsonViewFactory.create()
				.toJson();
        }

	
}